document.addEventListener('DOMContentLoaded', function() {
    // 主题切换功能
    const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
    const currentTheme = localStorage.getItem('theme');

    if (currentTheme) {
        document.documentElement.setAttribute('data-theme', currentTheme);
        if (currentTheme === 'dark') {
            toggleSwitch.checked = true;
        }
    }

    function switchTheme(e) {
        if (e.target.checked) {
            document.documentElement.setAttribute('data-theme', 'dark');
            localStorage.setItem('theme', 'dark');
        } else {
            document.documentElement.setAttribute('data-theme', 'light');
            localStorage.setItem('theme', 'light');
        }    
    }

    toggleSwitch.addEventListener('change', switchTheme, false);

    // 视频播放功能
    const videoItems = document.querySelectorAll('.video-item');

    videoItems.forEach(item => {
        const thumbnail = item.querySelector('.video-wrapper');
        const iframeContainer = item.querySelector('.video-iframe-container');
        const playerContainer = item.querySelector('.bilibili-player-container');

        thumbnail.addEventListener('click', function() {
            thumbnail.style.display = 'none';
            iframeContainer.style.display = 'block';

            const aid = playerContainer.getAttribute('data-aid');
            const bvid = playerContainer.getAttribute('data-bvid');
            const cid = playerContainer.getAttribute('data-cid');

            new BilibiliPlayer({
                aid: aid,
                bvid: bvid,
                cid: cid,
                element: playerContainer
            });
        });
    });

    // 课程分类过滤功能（如果需要）
    const categoryItems = document.querySelectorAll('.category-item');
    const courseCards = document.querySelectorAll('.course-card');

    categoryItems.forEach(item => {
        item.addEventListener('click', function(e) {
            e.preventDefault();
            const category = this.textContent.trim().toLowerCase();

            courseCards.forEach(card => {
                if (category === '全部' || card.dataset.category === category) {
                    card.style.display = 'block';
                } else {
                    card.style.display = 'none';
                }
            });

            // 更新活跃类别
            categoryItems.forEach(cat => cat.classList.remove('active'));
            this.classList.add('active');
        });
    });
});